package com.android.fragmentdemo.defineviews
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView

class DeOverScrollRecyclerView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : RecyclerView(context, attributeSet) {

    private var inDownOrUp = false

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.d(TAG, "onInterceptTouchEvent---MotionEvent.ACTION_DOWN")
            }

            MotionEvent.ACTION_MOVE -> {
                //parent.requestDisallowInterceptTouchEvent(inDownOrUp)
                Log.d(TAG, "onInterceptTouchEvent---MotionEvent.ACTION_MOVE")
            }

            MotionEvent.ACTION_UP,
            MotionEvent.ACTION_CANCEL -> {
                inDownOrUp = false
                Log.d(TAG, "onInterceptTouchEvent---MotionEvent.ACTION_UP")
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(e: MotionEvent?): Boolean {
        performClick()
        when (e?.action) {
            MotionEvent.ACTION_UP,
            MotionEvent.ACTION_CANCEL -> {
                inDownOrUp = false
            }
        }
        return super.onTouchEvent(e)
    }

    override fun performClick(): Boolean {
        return super.performClick()
    }

    override fun offsetChildrenVertical(dy: Int) {
        super.offsetChildrenVertical(dy)
        inDownOrUp = dy == 0
        Log.d(TAG, "dy:${dy}")
    }

    fun positionInDownOrUp() = inDownOrUp

    fun resetPosition(inDownOrUp: Boolean) {
        this.inDownOrUp = inDownOrUp
    }

    companion object {
        const val TAG = "DeOverScrollRecyclerView"
    }

}